package com.qf.air.search.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.air.search.entity.Air;
import com.qf.air.search.service.ElasticAirService;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/11/8 11:44
 */
@RestController
@RequestMapping("/search")
public class ElasticsearchController {

    @Autowired
    private ElasticAirService elasticAirService;

    @PostMapping(value = "/list")
    //public Map<String, Object> getSearchResponse(Integer page, Integer rows, Integer districtId, String monitoringStation, Date startTime, Date endTime) throws IOException {
    public Map<String, Object> getSearchResponse(
            //@RequestParam(defaultValue = "1") Integer page,
            //@RequestParam(defaultValue = "10") Integer rows,Integer districtId,String monitoringStation,
            //@DateTimeFormat(pattern = "yyyy-MM-dd")Date startTime,
            //@DateTimeFormat(pattern = "yyyy-MM-dd")Date endTime
            @RequestBody Map param
    ) throws IOException, InterruptedException {
        //SearchResponse response = elasticAirService.searchAir(page, rows, districtId, monitoringStation, startTime, endTime);
        Thread.sleep(500);
        SearchResponse response = elasticAirService.searchAir(param);
        List<Map<String, Object>> list=new ArrayList<>();
        for (SearchHit hit : response.getHits().getHits()) {
            HighlightField highlightField=hit.getHighlightFields().get("monitoringStation");
            Map<String, Object> map = hit.getSourceAsMap();
            if (highlightField!=null){
                String value=highlightField.getFragments()[0].toString();
                map.put("monitoringStation",value);
            }
            list.add(map);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",list);
        map.put("total",response.getHits().getTotalHits().value);
        return map;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void add(@RequestBody Map air) throws IOException {
        //ObjectMapper objectMapper=new ObjectMapper();
        //Air air = objectMapper.readValue(value, Air.class);
        elasticAirService.indexAir(air);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Integer id) throws IOException {
        elasticAirService.deleteAir(id);
    }

    @PutMapping("/update")
    public void update(@RequestBody Map air) throws IOException {
        elasticAirService.updateAir(air);
    }

    @GetMapping("/echarts")
    public List<Map> getDistrictNameEcharts() throws IOException {
        List<Map> districtNameEcharts = elasticAirService.getDistrictNameEcharts();
        Map map=new HashMap();
        map.put("name","air-search");
        map.put("version","2.2");
        districtNameEcharts.add(map);
        return districtNameEcharts;
    }

}
